package com.prs.crm.action.stock;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

import bsh.This;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.Inventory;
import com.prs.crm.domain.store.InventoryDetail;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.stock.InventoryService;
import com.prs.crm.service.stock.ProductService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreRoomService;

public class InventoryAction   extends FlowAction<Inventory>   {

	private static final long serialVersionUID = 1L;
	private Inventory inventory;

	private Product[] products;
	private ProductStore[] productStores;

	private Integer[] inventoryNum;

	private String[] remarks;
	private double[] numberInventorys;
	private String inventoryAttachFileName;
	
	private File upload;
	private String uploadFileName;
private String listString;
	@Autowired
	private InventoryService inventoryService;

	@Autowired
	private StoreRoomService storeRoomService;

	private PaginationSupport pagination;
	private StoreRoom room;

	private List<StoreRoom> storerooms;

	private Collection<InventoryDetail> slist;

	private String[] batchNumber;

	private List<Product> productlist;

	private ProductService productService;
	private ProductStoreService productStoreService;

	@FlowActive(target = "inventory", flowservice = "inventoryService")
	@Action("editInventory")
	public String editInventory() {
		this.storerooms = this.storeRoomService.getAllstoreroom();
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(target = "inventory", flowservice = "inventoryService")
	@Action(value = "saveInventory", results = {
			@Result(name = "success", type = "redirect", location = "listInventory"),
			@Result(name = "error", type = "chain", location = "editInventory"),
			@Result(name = "none", type = "redirect", location = "listInventory") })
	public String saveInventory() {
		if(inventory.getInventoryName()==null||inventory.getInventoryName().equals("")){
			this.addActionError("主题不能为空");
			return ERROR;
		}
		if(inventory.getInventoryDate()==null){
			this.addActionError("盘点时间不能为空");
			return ERROR;
		}if(inventory.getRoom()==null){
			this.addActionError("盘点仓库不能为空");
			return ERROR;
		}if(products==null){
			this.addActionError("盘点物品不能为空");
			return ERROR;
		}
		this.getInventoryService().saveInvenTory(inventory, 
				numberInventorys, upload, uploadFileName,
				 remarks,productStores);
		this.setPrompt("操作成功");
		return SUCCESS;
	}

	@Action("listInventory")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Inventory.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getInventoryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectProductStoreInventory")
	public String selectProductStoreInventory() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProductStore.class);
		criteria.add(Restrictions.eq("room", this.getRoom()));
		this.setPagination(this.getProductStoreService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("viewInventory")
	public String viewInventory() {
		inventory = this.getInventoryService().get(inventory.getId());
		this.slist = inventory.getDetails();
		this.displayEndNodes(inventory);
		return SUCCESS;
	}
	@Action("listInventoryFlowOver")
	public String listInventoryFlowOver() {
		pagination = this.getPaginationHelper().prepare(pagination);
		DetachedCriteria detachCriteria = this.getPaginationHelper()
				.createQuery(Inventory.class);
		detachCriteria.add(Restrictions.eq("flowstatus", 2)); 
		pagination = this.getInventoryService().pageQueryValid(detachCriteria,
				pagination);
		return SUCCESS;
	}
	@Action("editProcessInventory")
	public String editProcessInventory(){
		inventory = this.getInventoryService().get(inventory.getId());
		this.slist = inventory.getDetails();
		this.displayEndNodes(inventory);
		return SUCCESS;
	}
	
	@Action(value = "processInventory", results = {
			@Result(name = "success", type = "redirect", location = "listInventoryFlowOver"),
			@Result(name = "none", type = "redirect", location = "listInventoryFlowOver") })
	public String processInventory(){
		Set<InventoryDetail> inventoryDetails = inventory.getDetails();
		for (InventoryDetail detail : inventoryDetails) {
			ProductStore productStore = detail.getProductStore();
			String type = detail.getInventoryType();
			if (type.equals("盘盈")) {
				productStore.setProductNum(detail.getNumberInventory()
						+ detail.getInventoryNum());
			} else if (type.equals("盘亏")) {
				productStore.setProductNum(detail.getInventoryNum()
						- detail.getNumberInventory());
			}
			productStoreService.save(productStore);
		}
		inventory.setInventoryType("1");
		this.getInventoryService().save(inventory);
		return SUCCESS;
	}

	public Inventory getInventory() {
		return inventory;
	}

	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public ProductStore[] getProductStores() {
		return productStores;
	}

	public void setProductStores(ProductStore[] productStores) {
		this.productStores = productStores;
	}

	public Integer[] getInventoryNum() {
		return inventoryNum;
	}

	public void setInventoryNum(Integer[] inventoryNum) {
		this.inventoryNum = inventoryNum;
	}

	public String[] getRemarks() {
		return remarks;
	}

	public void setRemarks(String[] remarks) {
		this.remarks = remarks;
	}

	public double[] getNumberInventorys() {
		return numberInventorys;
	}

	public void setNumberInventorys(double[] numberInventorys) {
		this.numberInventorys = numberInventorys;
	}

	public String getInventoryAttachFileName() {
		return inventoryAttachFileName;
	}

	public void setInventoryAttachFileName(String inventoryAttachFileName) {
		this.inventoryAttachFileName = inventoryAttachFileName;
	}


	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public StoreRoom getRoom() {
		return room;
	}

	public void setRoom(StoreRoom room) {
		this.room = room;
	}

	public List<StoreRoom> getStorerooms() {
		return storerooms;
	}

	public void setStorerooms(List<StoreRoom> storerooms) {
		this.storerooms = storerooms;
	}

	public Collection<InventoryDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<InventoryDetail> slist) {
		this.slist = slist;
	}

	public String[] getBatchNumber() {
		return batchNumber;
	}

	public void setBatchNumber(String[] batchNumber) {
		this.batchNumber = batchNumber;
	}

	public List<Product> getProductlist() {
		return productlist;
	}

	public void setProductlist(List<Product> productlist) {
		this.productlist = productlist;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

}